package com.ekuaitu.kuaitu.payment;

import android.app.Activity;
import android.content.Intent;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.activity.InviteActivity;
import com.ekuaitu.kuaitu.activity.MainActivity;
import com.ekuaitu.kuaitu.bean.ChargeResultEvent;
import com.ekuaitu.kuaitu.bean.DepositBikeResultEvent;
import com.ekuaitu.kuaitu.bean.DepositCarResultEvent;
import com.ekuaitu.kuaitu.bean.MemberPayResultEvent;
import com.ekuaitu.kuaitu.bean.PaySuccessEvent;
import com.ekuaitu.kuaitu.utils.Constants;
import com.ekuaitu.kuaitu.utils.IntentKey;
import com.ekuaitu.kuaitu.utils.SharedPreferencesKey;
import com.ekuaitu.kuaitu.utils.SharedPreferencesUtils;
import com.ekuaitu.kuaitu.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by michal on 8/7.
 */
/*
* 统一处理支付的结果
* */
public class PayResult {
    /**
     * @param activity      微信是WXPayEntryActivity  支付宝是发起支付的Activity
     * @param payMethod     1微信  2支付宝 3个人账户
     * @param userPayType   0://缴纳汽车保证金     1://缴纳单车保证金   2://账户充值   3: //汽车订单支付    4//单车订单支付   5: //月季卡支付
     * @param payResultCode 0成功  1失败
     */
    public void handlePayResult(Activity activity, int payMethod, int userPayType, int payResultCode) {
        switch (userPayType) {  //判断支付什么
            case 0://缴纳汽车保证金
                EventBus.getDefault().post(new DepositCarResultEvent(payResultCode));
                if (payResultCode != 0) {
                    ToastUtils.toast(activity, R.drawable.task_error, "支付失败", R.layout.toast_layout);
                }
                break;
            case 1://缴纳单车保证金
                EventBus.getDefault().post(new DepositBikeResultEvent(payResultCode));
                if (payResultCode != 0) {
                    ToastUtils.toast(activity, R.drawable.task_error, "支付失败", R.layout.toast_layout);
                }
                break;
            case 2: //账户充值
                EventBus.getDefault().post(new ChargeResultEvent(payResultCode));
                if (payResultCode != 0) {
                    ToastUtils.toast(activity, R.drawable.task_error, "支付失败", R.layout.toast_layout);
                } else {
                    ToastUtils.toast(activity, R.drawable.task_done, "充值成功", R.layout.toast_layout);
                }
                break;
            case 3: //汽车订单支付
                EventBus.getDefault().post(new PaySuccessEvent(payResultCode));
                if (payResultCode != 0) {
                    ToastUtils.toast(activity, R.drawable.task_error, "支付失败", R.layout.toast_layout);
                } else {
                    ToastUtils.toast(activity, R.drawable.task_done, "支付成功", R.layout.toast_layout);
                    SharedPreferencesUtils.getInstance(activity).putIntValue(SharedPreferencesKey.USER_PAY_METHOD, payMethod);     //订单支付后，记录本次的支付方式，
                }

                break;
            case 4: //单车订单支付
                EventBus.getDefault().post(new PaySuccessEvent(payResultCode));
                if (payResultCode != 0) {
                    ToastUtils.toast(activity, R.drawable.task_error, "支付失败", R.layout.toast_layout);
                } else {
                    ToastUtils.toast(activity, R.drawable.task_done, "支付成功", R.layout.toast_layout);
                    SharedPreferencesUtils.getInstance(activity).putIntValue(SharedPreferencesKey.USER_PAY_METHOD, payMethod);     //订单支付后，记录本次的支付方式，
                }
                break;
            case 5: //月季卡支付
                EventBus.getDefault().post(new MemberPayResultEvent(payResultCode));
                if (payResultCode != 0) {
                    ToastUtils.toast(activity, R.drawable.task_error, "支付失败", R.layout.toast_layout);
                }
                break;
        }

        //微信支付结束EXPayEntryActivity
        if (payMethod == 1) {
            activity.finish();
        }
    }
}
